package com.dwxt.mall.controller;

import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.mall.entity.vo.OrderVO;
import com.dwxt.mall.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/mall/order")
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * showdoc
     * @catalog 商城/订单
     * @title 由状态分页查询订单
     * @description 由状态分页查询商城订单
     * @url /mall/order/selectMallOrderPage
     * @param pageNum 必选 int 每页数量
     * @param pageSize 必选 string 页码（1开始
     * @param type 必选 int 订单类型(商城订单:1,培训赛事:2)
     * @param status 可选 string 订单状态(下单,已支付,发货,收货,支付失败,申请退款,已退款,退款失败)
     * @return {"status":200,"msg":"SUCCESS","data":{"total":1,"list":[{"id":"1","name":"足球","number":10,"integral":1000,"realPrice":90.0,"status":"已支付","createTime":"2020-04-0715:07:29","payMethod":"WX","payTime":null,"productId":2,"addressId":1,"expireTime":null,"userId":"158502014539731","category":"体育用品","prepayId":"1","supplierId":8,"nonceStr":null,"receiptAddress":null,"product":{"id":null,"name":null,"price":null,"integral":null,"stock":null,"desp":"圆圆的足球","module":null,"categoryId":null,"type":null,"isShelf":null,"createTime":null,"supplierId":null,"payCategory":null,"version":null,"limitNumber":null,"isLimit":null,"priority":null,"deliveryMethod":null,"coverImgUrl":"http://demo.com","carouselImgUrl":null}}],"pageNum":10,"pageSize":0,"size":1,"startRow":1,"endRow":1,"pages":0,"prePage":0,"nextPage":0,"isFirstPage":false,"isLastPage":true,"hasPreviousPage":true,"hasNextPage":false,"navigatePages":8,"navigatepageNums":[],"navigateFirstPage":0,"navigateLastPage":0}}
     */
    @RequestMapping("/selectMallOrderPage")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0,notNull = true),
            @ValidateFiled(index = 1,notNull = true),
            @ValidateFiled(index = 2,notNull = true),
    })
    public BaseResult selectMallOrderPage(Integer pageNum, Integer pageSize, Integer type, String status) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderVO> payOrderVOList = orderService.selectMallOrderList(type, status);
        PageInfo<OrderVO> pageInfo = new PageInfo<>(payOrderVOList);
        return BaseResult.ok(pageInfo);
    }

    /**
     * showdoc
     * @catalog 商城/订单
     * @title 申请退款
     * @description 申请退款
     * @url /mall/order/reqRefund
     * @param orderId 必选 string 订单id
     * @param reason 可选 string 用户退款原因
     * @return {"status":200,"msg":"SUCCESS","data":null}
     */
    @RequestMapping("/reqRefund")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0,notNull = true),
    })
    public BaseResult reqRefund(String orderId,String reason){
        return orderService.reqRefund(orderId,reason);
    }

    @RequestMapping("/cancelRefund")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0,notNull = true),
    })
    public BaseResult cancelRefund(String orderId){
        return orderService.cancelRefund(orderId);
    }

    /**
     * showdoc
     * @catalog 商城/订单
     * @title 由主键查看订单详情
     * @description 由主键查看订单详情
     * @url /mall/order/selectDetailByKey
     * @param id 必选 string 订单id
     * @param type 必选 int 1,自己的2动美的
     * @return {"status":200,"msg":"SUCCESS","data":null}
     */
    @RequestMapping("/selectDetailByKey")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0,notNull = true),
            @ValidateFiled(index = 1,notNull = true),
    })
    public BaseResult selectDetailByKey(String id,Integer type){
        return orderService.selectDetailByKey(id,type);
    }


    /**
     * 未支付订单失效，取消订单，恢复库存
     * @param id
     * @return
     */
    @RequestMapping("/cancelOrder")
    public BaseResult cancelOrder(String id){
        orderService.cancelOrder(id);
        return BaseResult.ok();
    }
}
